package com.wealth.wechat.web.controller;

import iaas.annotation.Log;
import iaas.utils.RedirectMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.system.entity.Corporation;
import com.wealth.system.service.ICorpService;
import com.wealth.system.service.ShiroUser;
import com.wealth.system.web.controller.BaseController;
import com.wealth.wechat.entity.WechatConfig;
import com.wealth.wechat.service.WechatConfigService;

/** 
* 微信设置
* @author chan
*/
@Controller
@RequestMapping(value="/manage/wechat/config")
public class WechatConfigController extends BaseController{
	
	@Autowired
	private ICorpService corpService;
	@Autowired
	private WechatConfigService wechatConfigService;
	
	Logger logger = Logger.getLogger(WechatConfigController.class);
	
	@RequestMapping
	public String wechatForm(Model model){
		try{
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			//企业
			Corporation corp = corpService.get(corpid); 
			//企业微信配置
			WechatConfig config = wechatConfigService.getWechatConfigByCorpid(corpid);
			model.addAttribute("corp", corp);
			model.addAttribute("config", config);
			return "/wechat/config/index";
		}catch(Exception e){
			logger.error("微信设置页面错误", e);
			return "system/500";
		}
		
	}
	
	@Log(method="微信配置修改")
	@RequestMapping(method = RequestMethod.POST)
	public String updateWechat(Model model,WechatConfig config,RedirectAttributes redirectAttributes){
		try{
			wechatConfigService.saveOrUpdateWeChatCfg(config);
			redirectAttributes.addFlashAttribute("message",  RedirectMessage.setShowTipSuccess("微信设置操作成功"));
			return "redirect:/manage/wechat/config";
		}catch(Exception e){
			logger.error("微信设置错误", e);
			return "system/500";
		}
	}
}
